package com.yxh.lib_base.base.loading

import androidx.lifecycle.LifecycleOwner
import com.yxh.lib_base.base.viewmodel.BaseViewModel

interface ILoading {
    abstract fun showLoading(message: String)
    abstract fun dismissLoading()
}

abstract class ILoadingLifecycle(val lifecycle: LifecycleOwner): ILoading {
    /**
     * 将非该Activity绑定的ViewModel添加 loading回调 防止出现请求时不显示 loading 弹窗bug
     * @param viewModels Array<out BaseViewModel>
     */
    protected fun addLoadingObserve(vararg viewModels: BaseViewModel) {
        viewModels.forEach { viewModel ->
            //显示弹窗
            viewModel.loadingChange.showDialog.observe(lifecycle)  {
                showLoading(it)
            }
            //关闭弹窗
            viewModel.loadingChange.dismissDialog.observe(lifecycle)  {
                dismissLoading()
            }
        }
    }
}